package com.xinda.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinda.admin.pojo.Provider;
import com.xinda.admin.service.ProviderService;
import com.xinda.admin.vo.OrderEditVo;

@Controller
public class ProviderController {
	
	
	//1.providerList
	@Autowired
	private ProviderService providerService;
	
	@RequestMapping(value = "providerList")
	
	public String providerList(Model model) {
		
		List<Provider> providerlist = new ArrayList<Provider>();
		
		providerlist = providerService.listProvider();
		model.addAttribute("count",providerlist.size());
		model.addAttribute("providerList", providerlist);
		
		return "admin/provider/providerList";
		
	}


	
	//2.insert provider
	@RequestMapping(value = "providerAdd")
	public String providerAdd() {
		return "admin/provider/providerAdd";
	}
	
	@RequestMapping(value = "insertProvider")
	public void insertProvider(HttpServletRequest request ) {
		
		String name = request.getParameter("name");
		String providerInfo = request.getParameter("providerInfo");
		String cellphone = request.getParameter("cellphone");
		String password = request.getParameter("password");
		//int regionid = Integer.valueOf(request.getParameter("patient.City"));
		String providerImg = request.getParameter("providerImg");
		int status = Integer.valueOf(request.getParameter("status"));
		String qq = request.getParameter("qq");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String email = request.getParameter("email");
		int recommend = Integer.valueOf(request.getParameter("recommend"));
		
		String workTime = startTime + " -- " + endTime;
		
		/*
		Region region = new Region();                                            
		region.setId(regionid);
		*/
		Provider provider = new Provider();
		provider.setName(name);
		provider.setProviderInfo(providerInfo);
		provider.setCellphone(cellphone);
		provider.setPassword(password);
		//provider.setRegion(region);
		provider.setProviderImg(providerImg);
		provider.setStatus(status);
		provider.setQq(qq);
		provider.setWorkTime(workTime);
		provider.setEmail(email);
		provider.setRecommend(recommend);
		
		providerService.insertProvider(provider);
		
	}
	
	
	
	//3.view provider
	@RequestMapping(value = "providerView")
	public String providerView(Model model, HttpServletRequest request) {
		
		int id = Integer.valueOf(request.getParameter("id"));
		
		Provider provider = new Provider();
	    provider = providerService.readProvider(id);
	    //provider.addAttribute("provider", provider);
		model.addAttribute("provider", provider);
		
		return "admin/provider/providerView";
		
	}

	/*@RequestMapping(value = "EditProvider") 
	public void EditProvider(HttpServletRequest request ) {
		
		int id = Integer.valueOf(request.getParameter("id"));
		
		String name = request.getParameter("name");
		String providerInfo = request.getParameter("providerInfo");
		String cellphone = request.getParameter("cellphone");
		String password = request.getParameter("password");
		//int regionid = Integer.valueOf(request.getParameter("patient.City"));
		String providerImg = request.getParameter("providerImg");
		int status = Integer.valueOf(request.getParameter("status"));
		String qq = request.getParameter("qq");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String email = request.getParameter("email");
		int recommend = Integer.valueOf(request.getParameter("recommend"));
		
		String workTime = startTime + " -- " + endTime;
	
		
		Region region = new Region();                                            
		region.setId(regionid);
		
		
		//int id = Integer.valueOf(request.getParameter("id"));
		
		Provider provider = new Provider();
		provider .setId(id);
	 	provider.setName(name);
		provider.setProviderInfo(providerInfo);
		provider.setCellphone(cellphone);
		provider.setPassword(password);
		//provider.setRegion(region);
		provider.setProviderImg(providerImg);
		provider.setStatus(status);
		provider.setQq(qq);
		provider.setWorkTime(workTime);
		provider.setEmail(email);
		provider.setRecommend(recommend);
	
		provider.toString();
	
		providerService.editProvider(provider);
		
	}*/
	
	
	//4.delete provider
	@RequestMapping(value="providerDelete")
	@ResponseBody
	public void providerDelete(Integer id){
		providerService.deleteProvider(id);
	}
	
	
	//5.delect providers
	@RequestMapping(value ="deojfdg")
	@ResponseBody
	public void datadel(String[] checkbox) {
		providerService.deleteProviders(checkbox);
	}
	  
	
	 //6.deactivate provider
  	@RequestMapping(value = "providerDeactivate")
  	@ResponseBody
	public void providerDeactivate(Integer id) {
		providerService.deactivateProvider(id); //status;
	}
  	
		
	//7.start provider	
	@RequestMapping(value ="providerStart")
	@ResponseBody
	public void providerStart(Integer id) {
		providerService.startProvider(id);
	}

  
	//8.providerDeactivateList
	@RequestMapping(value ="providerDeactivateList")
	public String providerDeactivateList(Model model) {
		
		List<Provider> providerlist = new ArrayList<Provider>();
		
		providerlist = providerService.showProvider();
		model.addAttribute("count",providerlist.size());
		model.addAttribute("providerList", providerlist);
		
		return "admin/provider/providerDeactivateList";

	}
	
	//9.provider sell record
	@RequestMapping(value = "providerSellRecords")
	public String providerOrder(int id, Model model) {
		List<OrderEditVo> orderList = providerService.orderProvider(id);
		model.addAttribute("count", orderList.size());
		model.addAttribute("orderList", orderList);
		return "admin/provider/providerSellRecord";
	}

	
	/*
	 * 查看服务商
	 */
	/*	@RequestMapping(value = "providerShow")
	public String providerShow(Model model, Provider provider) {
		
		providerService.showProvider(provider);
		return "admin/provider/providerShow";
	}
	*/
}
